﻿using System;
using System.Collections.Generic;
using System.Text;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using System.Data.SqlClient;
using System.Data;
namespace NIIT.SaoBacDau.Aupi.HospitalManagement.DataAccess
{
    public class DAO_Pa_DetailsMedical
    {
        public static Pa_DetailsMedical DetailsMedical()
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("Patient.DetailsMedicalID", DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            Pa_DetailsMedical dm = new Pa_DetailsMedical();
            dm.DetailsMedicalID = dtable.Rows.Count;
            return dm;
        }
        public static Pa_DetailsMedical DetailsMedical(Pa_DetailsMedical pa_DetailsMedical)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("Patient.S_DetailsMedical", DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlda.SelectCommand.Parameters.Add("@DetailsMedicalID", SqlDbType.Int).Value = pa_DetailsMedical.DetailsMedicalID;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            Pa_DetailsMedical dm = new Pa_DetailsMedical();
            if (dtable.Rows.Count == 0)
                dm = null;
            else
            {
                dm.DetailsMedicalID = pa_DetailsMedical.DetailsMedicalID;
                dm.TypeMedicalID = Convert.ToInt32(dtable.Rows[0]["TypeMedicalID"].ToString());
                dm.PatientID = Convert.ToInt32(dtable.Rows[0]["TypeMedicalID"].ToString());
                if (string.IsNullOrEmpty(dtable.Rows[0]["TreatMentID"].ToString()) == false)
                    dm.TreatMentID = Convert.ToInt32(dtable.Rows[0]["TreatMentID"].ToString());
                dm.ContentMedical = dtable.Rows[0]["ContentMedical"].ToString();
            }
            return dm;
        }

        public static void AddDetailsMedical(Pa_DetailsMedical detailsMedical)
        {
            SqlConnection sc = DAO_ConnectDatabase.Connect();
            try
            {
                sc.Open();
                SqlCommand sqlcom = new SqlCommand("Patient.AddQuickDetailsMedical", sc);
                sqlcom.CommandType = CommandType.StoredProcedure;
                sqlcom.Parameters.Add("@DetailsMedicalID ", SqlDbType.Int).Value = detailsMedical.DetailsMedicalID;
                sqlcom.Parameters.Add("@PatientID", SqlDbType.Int).Value = detailsMedical.PatientID;
                sqlcom.ExecuteNonQuery();
            }
            finally
            {
                if (sc.State == ConnectionState.Open)
                    sc.Close();
            }
        }

        public static List<Pa_DetailsMedical> ListDetailsMedical(int value, int to)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("[Patient].[P_DetailsMedical]", DAO_ConnectDatabase.Connect());
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            List<Pa_DetailsMedical> list = new List<Pa_DetailsMedical>();
            foreach (DataRow dr in dtable.Rows)
            {
                Pa_DetailsMedical p_DetailsMedical = new Pa_DetailsMedical();
                p_DetailsMedical.DetailsMedicalID = Convert.ToInt32(dr["DetailsMedicalID"].ToString());
                p_DetailsMedical.TypeMedicalID = Convert.ToInt32(dr["TypeMedicalID"].ToString());
                p_DetailsMedical.PatientID = Convert.ToInt32(dr["PatientID"].ToString());
                if(string.IsNullOrEmpty(dr["TreatMentID"].ToString())== false)
                    p_DetailsMedical.TreatMentID = Convert.ToInt32(dr["TreatMentID"].ToString());
                p_DetailsMedical.ContentMedical = dr["ContentMedical"].ToString();
                list.Add(p_DetailsMedical);
            }
            return list;
        }

        public static Pa_DetailsMedical DetailsMedical(Pa_Patient Pa_patient)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("[Patient].[Pa_DetailsMedical]", DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlda.SelectCommand.Parameters.Add("@PatientID", SqlDbType.Int).Value = Pa_patient.PatientID;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            Pa_DetailsMedical pa_DetailsMedical = new Pa_DetailsMedical();
            if (dtable.Rows.Count == 0)
                pa_DetailsMedical = null;
            else
            {
                pa_DetailsMedical.DetailsMedicalID = Convert.ToInt32(dtable.Rows[0]["DetailsMedicalID"].ToString());
                pa_DetailsMedical.TypeMedicalID = Convert.ToInt32(dtable.Rows[0]["TypeMedicalID"].ToString());
                pa_DetailsMedical.PatientID = Convert.ToInt32(dtable.Rows[0]["PatientID"].ToString());
                if (string.IsNullOrEmpty(dtable.Rows[0]["TreatMentID"].ToString())== false)
                    pa_DetailsMedical.TreatMentID = Convert.ToInt32(dtable.Rows[0]["TreatMentID"].ToString());
                pa_DetailsMedical.ContentMedical = dtable.Rows[0]["ContentMedical"].ToString();
            }
            return pa_DetailsMedical;
        }
    }
}
